package ru.weather.common;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import ru.weather.common.responseData.CorrectResponse;
import ru.weather.common.responseData.IncorrectResponse;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static ru.weather.common.AssertUtils.*;
import static ru.weather.common.Data.BASE_URL;
import static ru.weather.common.Data.KEY_API;

/**
 * Шаги тестов
 */
public class Steps {

    public Response sendRequestForWeatherInCityStep(String cityName){

        String endpoint = BASE_URL+"/current.json?key="+KEY_API+"&q="+cityName;
        RequestSpecification requestSpecification =  RestAssured.given().contentType("application/json");
        return requestSpecification.when().post(endpoint);
    }

    public void checkAnswerStep(Response response,String cityName){

        CorrectResponse resp = response.then().extract().as(CorrectResponse.class);
        assertLocation(resp,cityName);
        assertCurrent(resp);

    }

    public Response sendRequestWithNoAPIKeyStep(){
        RequestSpecification requestSpecification =  RestAssured.given().contentType("application/json");
        return requestSpecification.when().post(BASE_URL+"/current.json");
    }

    public void answerStep(int status, Response response, int code){
        int responseCode = response.then().extract().statusCode();
        IncorrectResponse resp = response.then().extract().as(IncorrectResponse.class);
        assertEquals(status, responseCode, "status code must be "+status);
        assertEquals(code, resp.getError().getCode(),"code must be "+code);
    }

    public Response sendRequestWithNoQParameterStep(){
        RequestSpecification requestSpecification =  RestAssured.given().contentType("application/json");
        return requestSpecification.when().post(BASE_URL+"/current.json?key="+KEY_API+"&q=");
    }

    public Response sendRequestWithWrongQParameterStep(){
        RequestSpecification requestSpecification =  RestAssured.given().contentType("application/json");
        return requestSpecification.when().post(BASE_URL+"/current.json?key="+KEY_API+"&q=asldk1781lkjasd");
    }

    public Response sendRequestWithAPIKeyHasBeenDisabledStep(){
        RequestSpecification requestSpecification =  RestAssured.given().contentType("application/json");
        return requestSpecification.when().post(BASE_URL+"/current.json?key="+KEY_API+"?q=Paris");
    }
}
